﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace FluentResourceMapping.Web.Mvc
{
    /// <summary>
    /// Extends the default ASP.NET MVC metadata provider to provide resource metadata for mapped classes.
    /// </summary>
    public class FluentResourceMappingMetadataProvider : DataAnnotationsModelMetadataProvider
    {
        private readonly ResourceMappingModel resourceMapping;

        public FluentResourceMappingMetadataProvider(ResourceMappingModel resourceMapping) //TODO usar interface
        {
            this.resourceMapping = resourceMapping;
        }

        protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType,
                                                        Func<object> modelAccessor, Type modelType, string propertyName)
        {
            ModelMetadata metadata = base.CreateMetadata(attributes, containerType, modelAccessor, modelType,
                                                         propertyName);

            string displayName = resourceMapping.GetResource(containerType, propertyName);

            if (displayName != null)
                metadata.DisplayName = displayName;

            return metadata;
        }
    }
}
